import {createStoreBindings} from "mobx-miniprogram-bindings";
import {timStore} from "../../store/tim";
import Tim from "../../model/tim";

Page({
  data: {
    targetUserId: null,
    service: null
  },
  onLoad: function (options) {
    this.storeBindings = createStoreBindings(this, {
      store: timStore,
      fields: ['sdkReady'],// 要绑定哪个状态,会绑定到data里
      actions: ['pushMessage', 'resetMessage']
    })
    const targetUserId = options.targetUserId
    this.setData({
      // targetUserId: targetUserId,
      targetUserId: 'user1',
      service: options.service ? JSON.parse(options.service) : null
    })
  },

  handSendMessage(event) {
    const {type, content} = event.detail
    const message = Tim.getInstance().createMessage(type, content, this.data.targetUserId)
    this.pushMessage(message)
    Tim.getInstance().sendMessage(message)
    this.getOpenerEventChannel().emit('sendMessage')
  },

  handleLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },

  onUnload() {
    this.resetMessage()
    this.storeBindings.destroyStoreBindings()
  }
})